package org.sith.taskrunner.ejb.db.entity;

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "TOOLKITS")
@NamedQueries({@NamedQuery(name = "findByNameAndVersion", query = "select tte from TaskToolkitEntity tte where tte.toolkitName=:toolkitName and tte.version=:version"),
        @NamedQuery(name = "findActiveToolkits", query = "select tte from TaskToolkitEntity tte where tte.active=true and tte.system=false"),
        @NamedQuery(name = "findSystemToolkits", query = "select tte from TaskToolkitEntity tte where tte.active=true and tte.system=true"),
        @NamedQuery(name = "updateToolkitActiveState", query = "update TaskToolkitEntity tte set tte.active=:active where tte.id=:id"),
        @NamedQuery(name = "updateAllToolkitActiveState", query = "update TaskToolkitEntity tte set tte.active=:active"),
        @NamedQuery(name = "updateAllToolkitActiveStateByClassName", query = "update TaskToolkitEntity tte set tte.active=:active where tte.toolkitClazz=:clazz"),
        @NamedQuery(name = "findActiveToolkit", query = "select tte from TaskToolkitEntity tte where tte.toolkitName=:name and tte.version=:version"),
        @NamedQuery(name = "findAllToolkits", query = "select tte from TaskToolkitEntity tte where tte.system=false")})

public class TaskToolkitEntity implements DBEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "VERSION", nullable = false)
    private Double version;

    @Column(name = "TOOLKIT_NAME")
    private String toolkitName;

    @Column(name = "TOOLKIT_CLASS", nullable = false)
    private String toolkitClazz;

    @Column(name = "IS_ACTIVE")
    private boolean active;

    @Column(name = "REGISTER_DATE")
    @Temporal(TemporalType.TIMESTAMP)
    private Date registerDate;

    @Column(name = "DESCRIPTION")
    private String description;

    @Column(name = "IS_SYSTEM")
    private boolean system;


    @ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
    @JoinColumn(name = "USER_ID")
    private UserEntity deployer;

    @Column(name = "IS_WRAPPER")
    private boolean wrapper;

    public UserEntity getDeployer() {
        return deployer;
    }

    public void setDeployer(UserEntity deployer) {
        this.deployer = deployer;
    }

    public boolean isSystem() {
        return system;
    }

    public void setSystem(boolean system) {
        this.system = system;
    }

    @Override
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getToolkitName() {
        return toolkitName;
    }

    public void setToolkitName(String builderName) {
        this.toolkitName = builderName;
    }

    public String getToolkitClazz() {
        return toolkitClazz;
    }

    public void setToolkitClazz(String builderClazz) {
        this.toolkitClazz = builderClazz;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public Date getRegisterDate() {
        return registerDate;
    }

    public void setRegisterDate(Date registerDate) {
        this.registerDate = registerDate;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        TaskToolkitEntity that = (TaskToolkitEntity) o;

        if (!toolkitClazz.equals(that.toolkitClazz)) return false;
        if (!toolkitName.equals(that.toolkitName)) return false;
        if (!version.equals(that.version)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = version.hashCode();
        result = 31 * result + toolkitName.hashCode();
        result = 31 * result + toolkitClazz.hashCode();
        return result;
    }

    public Double getVersion() {
        return version;
    }

    public void setVersion(Double version) {
        this.version = version;
    }

    public boolean isWrapper() {
        return wrapper;
    }

    public void setWrapper(boolean wrapper) {
        this.wrapper = wrapper;
    }
}
